package org.video.mine.fragment.home;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.video.mine.R;
import org.video.mine.Urls;
import org.video.mine.adapter.MovieInfoAdapter;
import org.video.mine.databinding.FirstFragmentBinding;
import org.video.mine.entity.MovieInfoEntity;
import org.video.mine.mycode.DownTask;
import org.video.mine.mycode.DownThread;
import org.video.mine.network.JsoupGet;

import java.util.ArrayList;
import java.util.List;

public class FirstFragment extends Fragment {

    private FirstViewModel mViewModel;
    private FirstFragmentBinding binding;
    private ViewModelStoreOwner owner;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    infoAdapter.notifyDataSetChanged();
                    binding.refreshLayout.finishRefresh();
                    break;
            }
        }
    };
    private List<MovieInfoEntity> movieInfo = new ArrayList<>();
    private MovieInfoAdapter infoAdapter;

    public static FirstFragment newInstance() {
        return new FirstFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.first_fragment,container,false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(FirstViewModel.class);
        owner = this;
        // TODO: Use the ViewModel
        initView();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                movieInfo.clear();
                movieInfo.addAll(JsoupGet.getMovieInfo(owner, Urls.homeUrl0));
                mHandler.sendEmptyMessage(0);
            }
        }).start();
    }

    private void initView(){
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (TextUtils.isEmpty(movieInfo.get(position).getImgUrl())){
                    return 2;
                }else {
                    return 1;
                }
            }
        });
        binding.cyclerView.setLayoutManager(gridLayoutManager);
        infoAdapter = new MovieInfoAdapter(movieInfo, getContext(), "first");
        binding.cyclerView.setAdapter(infoAdapter);

        binding.refreshLayout.setEnableLoadMore(false);
        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
            }
        });
        initData();
    }
}
